package cn.yqx.bankshop.user.service.impl;

import cn.yqx.bankshop.model.*;
import cn.yqx.bankshop.user.dao.BuUserInfoMapper;
import cn.yqx.bankshop.user.dao.SysPermissionMapper;
import cn.yqx.bankshop.user.pojo.BuUserInfo;
import cn.yqx.bankshop.user.pojo.BuUserInfoExample;
import cn.yqx.bankshop.user.pojo.LoginShopUser;
import cn.yqx.bankshop.user.service.RoleUserService;
import cn.yqx.bankshop.user.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    BuUserInfoMapper buUserInfoMapper ;
    @Resource
    RoleUserService roleUserService ;
    @Autowired
    SysPermissionMapper sysPermissionMapper ;

    @Override
    public BuUserInfo selectByUsername(String username) {

        BuUserInfoExample buUserInfoExample = new BuUserInfoExample() ;
        buUserInfoExample.or().andUsernameEqualTo(username) ;
        List<BuUserInfo> list = buUserInfoMapper.selectByExample(buUserInfoExample) ;
        log.info("LIST IS : {} " , list );
        if(!CollectionUtils.isEmpty(list)){
            return list.get(0);
        }

        BuUserInfoExample buUserInfoExample2 = new BuUserInfoExample() ;
        buUserInfoExample2.or().andPhoneEqualTo(username) ;
        list = buUserInfoMapper.selectByExample(buUserInfoExample2) ;
        if(CollectionUtils.isEmpty(list)){
            return null ;
        }
        log.info("LIST IS : {} " , list );
        return list.get(0);

    }

    @Override
    public BuUserInfo selectByUserId(Long userId) {

        return buUserInfoMapper.selectByPrimaryKey(userId);
    }

    @Override
    public BuUserInfo selectByUserIdNoPassword(Long userId) {
        BuUserInfo buUserInfo = buUserInfoMapper.selectByPrimaryKey(userId);
        buUserInfo.setPassword("");
        return buUserInfo;
    }

    @Override
    public LoginShopUser findByUsername(String username) {

        BuUserInfoExample buUserInfoExample = new BuUserInfoExample() ;
        buUserInfoExample.or().andAccountEqualTo(username) ;
        BuUserInfo userInfo = buUserInfoMapper.selectByExample(buUserInfoExample).get(0) ;

        //添加权限
        LoginShopUser loginShopUser = this.getLoginAppUser(userInfo);

        return loginShopUser;
    }

    @Override
    public LoginShopUser findByMobile(String mobile) {

        BuUserInfoExample buUserInfoExample = new BuUserInfoExample() ;
        buUserInfoExample.or().andPhoneEqualTo(mobile) ;
        BuUserInfo userInfo = buUserInfoMapper.selectByExample(buUserInfoExample).get(0) ;
        //添加权限
        LoginShopUser loginShopUser = this.getLoginAppUser(userInfo);

        return loginShopUser;
    }

    @Override
    public LoginShopUser findByOpenId(String openId) {
        BuUserInfoExample buUserInfoExample = new BuUserInfoExample() ;
        buUserInfoExample.or().andOpenidEqualTo(openId) ;
        BuUserInfo userInfo = buUserInfoMapper.selectByExample(buUserInfoExample).get(0) ;

        //添加权限
        LoginShopUser loginShopUser = this.getLoginAppUser(userInfo);

        return loginShopUser;
    }

    @Override
    public int editUserinfoByUserid(BuUserInfo userInfo) {
        return buUserInfoMapper.updateByPrimaryKeySelective(userInfo);
    }

    public LoginShopUser getLoginAppUser(BuUserInfo buUserInfo) {
        if (buUserInfo != null) {
            LoginShopUser loginShopUser = new LoginShopUser();
            BeanUtils.copyProperties(buUserInfo, loginShopUser);

            List<SysRole> sysRoles = roleUserService.findRolesByUserId(buUserInfo.getId());
            // 设置角色
            loginShopUser.setRoles(sysRoles);

            if (!CollectionUtils.isEmpty(sysRoles)) {
                for(SysRole sysRole : sysRoles){
                    List<SysPermission> sysPermissions = sysPermissionMapper.selectPermissionByRoleId(sysRole.getId());
                    if(!CollectionUtils.isEmpty(sysPermissions)){
                        Set<String> permissions = sysPermissions.parallelStream().map(p -> p.getCode())
                                .collect(Collectors.toSet());
                        loginShopUser.setPermissions(permissions);
                    }
                }
            }
            return loginShopUser;
        }
        return null;
    }
}
